package com.zhaohang.cn.zhaohangapp;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhaohang.cn.constant.ConstantUtils;
import com.zhaohang.cn.uimarger.UiChengeAuto;
import com.zhy.autolayout.AutoLinearLayout;


/**
 * 作者: 赵航
 * 创建时间: 2018/10/18 19:00
 * 描述:
 */
public abstract class BasicFragment extends Fragment {


    private RelativeLayout   mLeft;
    private TextView         mTitleText;
    private RelativeLayout   mRight;
    private AutoLinearLayout mFragmentHand;
    private AutoLinearLayout     mContentLayout;
    private LayoutInflater mInflater;
    private ViewGroup mView;
    private SmartRefreshLayout mSmartRefreshLayout;
    private BasicAllActivity mActivity;
    private AutoLinearLayout mFragmentBar;
    private View mMainView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mMainView = inflater.inflate(R.layout.fragment_basic, null);
        mLeft = (RelativeLayout) mMainView.findViewById(R.id.left);
        mRight = (RelativeLayout) mMainView.findViewById(R.id.right);
        mTitleText = (TextView) mMainView.findViewById(R.id.titletext);
        mFragmentHand = (AutoLinearLayout) mMainView.findViewById(R.id.fragmenthand);
        mContentLayout = (AutoLinearLayout) mMainView.findViewById(R.id.fragemtcontent_layout);
        mActivity = (BasicAllActivity) getActivity();
        mFragmentBar=(AutoLinearLayout) mMainView.findViewById(R.id.fragemtactionbar);
        initView();

        showFragmentState(setFragmentState());
        return mMainView;
    }



    private void showFragmentState(int  states) {
        switch (states) {
            case ConstantUtils.LINEARSHOW:
                mFragmentHand.setVisibility(View.VISIBLE);
                mFragmentBar.setVisibility(View.VISIBLE);
                break;
            case ConstantUtils.RELEATSHOW:
                mFragmentBar.setVisibility(View.GONE);
                mFragmentHand.setVisibility(View.GONE);
                break;
        }

    }

    private void initView() {
        mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //初始化view
        if (null != mContentLayout) {
            mView = (ViewGroup) mInflater.inflate(setFragemtSuccessView(), null);
            LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            mContentLayout.addView(mView, mLayoutParams);

            initChildView(mView);

            ViewGroup mNoNetView = mView.findViewById(setFragemtNoNetView());


            //如果在无网状态下
            if (!mActivity.isConnected()) {
                //指定无网络在哪个布局上进行显示
                if (mNoNetView != null) {
                    View mChild = mInflater.inflate(R.layout.nonet_layout, null);
                    LinearLayout.LayoutParams mChildParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.MATCH_PARENT);

                    for (int i = 0; i < mNoNetView.getChildCount(); i++) {
                        mNoNetView.getChildAt(i).setVisibility(View.GONE);
                    }

                    mNoNetView.addView(mChild, mChildParams);
                    initNoNetView(mChild, mNoNetView);
                }
            } else {

                initFragemntData();

                showViewWitheData();
            }
        }
    }

    protected abstract void initChildView(ViewGroup view);

    private void showViewWitheData() {


        final View view = mView.findViewById(setNoDataFragmentView());
        if (view != null) {

            UiChengeAuto chengeAuto = new UiChengeAuto(getContext()) {
                @Override
                protected View getSuccessView() {
                    mView.removeView(view);
                    return  view;
                }
                @Override
                protected Object getNetData() {
                    return setFragmentData();
                }
            };
            mView.addView(chengeAuto);
        }


    }
    private void initNoNetView(final View mChild, final ViewGroup mNoNetView) {
        mSmartRefreshLayout = (SmartRefreshLayout) mChild.findViewById(R.id.refreshLayout);
        //下拉刷新
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
//                mSmartRefreshLayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                if (!mActivity.isConnected()) {
                    mSmartRefreshLayout.finishRefresh();
                    return;
                } else {
                    mNoNetView.removeView(mChild);
                    for (int i = 0; i < mNoNetView.getChildCount(); i++) {
                        mNoNetView.getChildAt(i).setVisibility(View.VISIBLE);
                    }
                    mSmartRefreshLayout.finishRefresh();
                    showViewWitheData();
                }
            }
        });
    }

    /**
     *
     * @return 无网状态在哪个界面进行展示
     */
    protected abstract int setFragemtNoNetView();

    /**
     *
     * @return 加载Fragment中的成功布局
     */
    protected abstract int setFragemtSuccessView();

    /**
     *
     * @return 是否显示标题栏
     */
    protected abstract int setFragmentState();

    /**
     *
     * @return 展示无数据的界面
     */
    protected abstract int setNoDataFragmentView();

    /**
     *
     * @return 根据数据来进行显示
     */
    protected abstract Object setFragmentData();

    /**
     * 初始化各个VIew控件
     */

    /**
     * 进行获取数据
     */
    protected abstract void initFragemntData();

    /**
     * @return 线性布局的头布局
     */
    public LinearLayout getHandLinear() {
        if (mFragmentHand == null) {
            mFragmentHand = (AutoLinearLayout) mMainView.findViewById(R.id.fragmenthand);
        }
        return mFragmentHand;
    }
    /**
     * 获取最顶部导航栏
     */
    public LinearLayout getFragmentBar(){
        if (mFragmentBar==null){
            mFragmentBar=(AutoLinearLayout) mMainView.findViewById(R.id.fragemtactionbar);
        }
        return mFragmentBar;
    }

    /**
     * 设置左边按钮里是文字 setLeftShow(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @throws
     * @since 1.0.0
     */
    public void setLeftShow(int tag, int resID) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        if (null == mLeft) {
            mLeft = (RelativeLayout) mMainView.findViewById(R.id.left);
        }
        mLeft.removeAllViews();
        mLeft.setVisibility(View.VISIBLE);
        if (tag == 0) {
            View view = getLayoutInflater().inflate(R.layout.textview, null);
            TextView text = view.findViewById(R.id.text);
            text.setText(this.getResources().getString(resID));

            mLeft.addView(view, params);
        } else if (tag == 1) {
            View view = getLayoutInflater().inflate(R.layout.imageview, null);
            ImageView imageView = view.findViewById(R.id.icon);
            imageView.setImageResource(resID);
            mLeft.addView(view, params);
        } else if (tag == 3) {
            View view = getLayoutInflater().inflate(R.layout.weightback, null);
            mLeft.addView(view, params);
        }
    }

    public void setTitleText(String title) {

        if (mTitleText==null){
            mTitleText = (TextView) mMainView.findViewById(R.id.titletext);
        }
        mTitleText.setText(title);
    }
    /**
     * 设置右边边按钮里是文字 setRightShow(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @throws
     * @since 1.0.0
     */
    public void setRightShow(int tag, int resID) {
        if (null == mRight) {
            mRight = (RelativeLayout) mMainView.findViewById(R.id.right);
        }
        mRight.removeAllViews();
        mRight.setVisibility(View.VISIBLE);
        if (tag == 0) {
            View view = getLayoutInflater().inflate(R.layout.textview, null);
            TextView text = view.findViewById(R.id.text);
            text.setText(this.getResources().getString(resID));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mRight.addView(view, params);
        } else if (tag == 1) {
            View view = getLayoutInflater().inflate(R.layout.imageview, null);
            ImageView imageView = view.findViewById(R.id.icon);
            imageView.setImageResource(resID);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            mRight.addView(view, params);
        }
    }
}
